import { showLoading } from '$components/basic/message';
// import html2canvas from 'html2canvas';
// import jsPDF from 'jspdf';

export const exportDomToImageById = async (
	id: string | HTMLElement,
	callback?: (dataUrl: string, img?: HTMLCanvasElement) => void
) => {
	const res = await import('html2canvas');
	console.log(res);
	const html2canvas = res.default;
	const msg = showLoading();
	const dom = typeof id === 'string' ? document.getElementById(id) : id;
	if (!dom) return msg.error();
	html2canvas(dom).then((canvas) => {
		// const dataUrl = canvas.toDataURL();
		// if (callback) callback(dataUrl, canvas);
		if (callback) callback('', canvas);
		setTimeout(() => msg.close(), 500);
	});
};

export const exportPngToPDF = async (callback?: () => [string, HTMLCanvasElement]) => {
	const { jsPDF } = await import('jspdf');
	const pdf = new jsPDF({
		orientation: 'p', // 纵向排列
		unit: 'mm', // 单位为毫米
		format: 'a4' // A4 页面尺寸
	});

	if (callback)
		(() => {
			const [fileName, canvas] = callback();
			pdf.addImage(canvas, 'JPEG', 0, 0, 210, 297);
			pdf.save(fileName || 'download.pdf');
		})();
};
